package com.lmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmj.dto.DataRequest;
import com.lmj.dto.DataResponse;
import com.lmj.mapper.UserMapper;
import com.lmj.pojo.CurDictionary;
import com.lmj.pojo.User;
import com.lmj.service.UserService;
import com.lmj.utils.RequestBody;
import com.lmj.utils.MD5Util;
import com.lmj.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    TokenUtil tokenUtil;

    // 响应体
    private DataResponse dataResponse;
    // 响应数据
    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse loginByPassword(String request) {

        // 获取请求体
        DataRequest requestBody = RequestBody.getRequestBody(request);

        // 清空map
        map.clear();

        // 手机号
        String userPhone = requestBody.getString("userPhone");
        // 密码
        String userPassword = requestBody.getString("userPassword");

        // 手机号为空、密码为空
        if (userPhone == null || userPhone.equals("")) {
            map.put("error", "手机号为空");
            dataResponse = new DataResponse("400", map);
            return dataResponse;
        } else if (userPassword == null || userPassword.equals("")) {
            map.put("error", "密码为空");
            dataResponse = new DataResponse("400", map);
            return dataResponse;
        } else {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUserPhone, userPhone);
            User user = userMapper.selectOne(queryWrapper);

            // 在数据库查找是否有这个用户
            if (user != null) {

                //  在数据库比对密码
                String MD5Password = MD5Util.string2MD5(userPassword);
                // 比较加密后的结果
                if (!user.getUserPassword().equals(MD5Password)){
                    map.put("error", "密码错误");
                    dataResponse = new DataResponse("400", map);
                    return dataResponse;
                }

                // 将用户手机号存入token，方便验证登录状态
                String token = tokenUtil.buildToken(userPhone.toString());
                System.out.println("该用户生成的token：");
                System.out.println(token);
                tokenUtil.setToken(userPhone.toString(), token);

                map.put("userPhone", user.getUserPhone());

                dataResponse = new DataResponse("200", map);
                return dataResponse;

            } else {
                map.put("error", "该用户不存在");
                dataResponse = new DataResponse("400", map);
                return dataResponse;
            }
        }
    }

    @Override
    public DataResponse getUserInfo(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);
        String userPhone = requestBody.getString("userPhone");

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserPhone, userPhone);
        User user = userMapper.selectOne(queryWrapper);

        map.clear();

        map.put("userName", user.getUserName().toString());
        map.put("userStatusId", user.getUserStatusId().toString());

        return new DataResponse("200", map);
    }

}
